<template>
  <div class="app-container checkContainer">
    <div class="customContainer">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <dj-store-select :category="581" :defaultStore="formInline.store_id" :needItem="true" @change="selectStoreChanged" />
        <el-form-item>
          <el-date-picker v-model="pickerDate" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="daterange" align="right" unlink-panels range-separator="-"
            start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select filterable placeholder="兑换方式" v-model="formInline.type_id">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
      </el-form>
      <!-- 列表 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="mini">
        <el-table-column align="center" label="序号">
          <template slot-scope="scope">
            <b>{{scope.$index + 1  + (currentPage - 1) * 20}}</b>
          </template>
        </el-table-column>
        <el-table-column align="center" label="兑换时间" prop="create_time"> </el-table-column> 
        <el-table-column align="center" label="订单编号" prop="trade_no"></el-table-column>
        <el-table-column align="center" label="会员卡号" prop="card_no"></el-table-column>
        <el-table-column align="center" label="用户名" prop="user_name"></el-table-column>
        <el-table-column align="center" label="手机号" prop="mobile"></el-table-column>
        <el-table-column align="center" label="兑换礼品名称" prop="gift_name"></el-table-column>
        <el-table-column align="center" label="礼品编号" prop="gift_id"></el-table-column>
        <el-table-column align="center" label="神采编号" prop="no_obsolete"></el-table-column>
        <el-table-column align="center" label="兑换方式" prop="type_id">
          <template slot-scope="scope">
            {{scope.row.type_id | returnType}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="兑换数（个）" prop="num"></el-table-column>
        <el-table-column align="center" label="成本价（元）" prop="money_cost">
          <template slot-scope="scope">
            {{scope.row.money_cost | keepTwoNum}}
          </template>
        </el-table-column> 
        </el-table-column>
        <el-table-column align="center" label="出库金额（元）" prop="out_money">
          <template slot-scope="scope">
            {{scope.row.out_money | keepTwoNum}}
          </template>
        </el-table-column> 
      </el-table> 
    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="20"
        layout="prev, pager, next, jumper" :total="totalDataNumber">
      </el-pagination>
      <span class="pull-right mt10"
        style="font-size:14px;color:#606266">　兑换数量合计：{{returnNum}}&#x3000;&#x3000;出库金额合计：{{returnMoney|keepTwoNum}}&#x3000;&#x3000;</span>

    </div>
  </div>
</template>
<script>
import { getPage,getList } from '@/api/report/gift_change_out_form';
import { pickerOptionsF, pickerDateF,pickerDateDot, optionMaxTime,getDateString } from '@/utils';
// import { DjSimpleTable } from '@/components/table';
import { DjStoreSelect } from '@/components/store';
export default {
  components: {
    // 'dj-simple-table': DjSimpleTable,
    'dj-store-select': DjStoreSelect
  },
  data() {
    const typeList = [
      {
        id: '0',
        name: '彩票兑换'
      },
      {
        id: '1',
        name: '积分兑换'
      }, 
    ];
    return {
      list: null,
      listLoading: false, 
      typeList: typeList,
      storeName: null,
      currentPage: 1,
      totalDataNumber: 0,
      formInline: {
        store_id: this.$store.getters.optionId,
        begin_time: '',
        end_time: '',
        type_id: '0',
        look_del: '0'
      },
      pickerOptions: pickerOptionsF(-1),
      pickerDate: pickerDateDot(-1,-1),
      returnNum: 0,
      returnMoney: 0
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => { 
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      vm.beforeComingIn();
    });
  },
  methods: {
    beforeComingIn() {
      if (this.$route.query.routerflag) {
        // console.log(this.$route.query);
        let col = this.$route.query.col;
        // 本月第一天至昨天
        let fist_day = getDateString('this_month_first_day', this.$route.query.global.time);
        let date = new Date(this.$route.query.global.time);
        let now = new Date();
        let yesterday;
        if (date.getMonth() == now.getMonth()) {
          yesterday = getDateString('yesterday');
        } else {
          yesterday = getDateString('this_month_last_day', this.$route.query.global.time);
        }
        this.pickerDate = [fist_day, yesterday];
        this.formInline.store_id = this.$route.query.global.store_id;

        // this.searchClick();
      } else {
        if (!this.listData || this.listData.length == 0) {
          this.searchClick();
        }
      }
    },
    selectStoreChanged(obj, item) { 
      for (let key in obj) this.formInline[key] = obj[key];
      this.storeName = item.name;
      this.searchClick();
    },
    // 搜索
    searchClick() {
      optionMaxTime(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.begin_time = this.pickerDate ? this.pickerDate[0] : '';
        this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : '';
        this.fetchData();
      });
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then(response => { 
          this.totalDataNumber = response.data.page * 20;
          this.returnNum = response.data.mun ? response.data.mun : 0
          this.returnMoney = response.data.money
          getList(this.formInline,this.currentPage).then(response => {  
            this.list = response.data;
            this.listLoading = false;
          });
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    // 分页 
    handleCurrentChange(val) {
      this.listLoading = true;
      this.currentPage = val;
       getList(this.formInline,this.currentPage).then(response => { 
            this.list = response.data;
            this.listLoading = false;
          })
        .catch(res => {
          this.listLoading = false;
        });
    }
  },
  filters:{
    // 兑换方式
    returnType(value) {
      if(value === '0') {
        return '彩票兑换'
      } else {
        return '积分兑换'
      }
    }
  }
};
</script>